package com.app.library.ext

import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import android.widget.ImageView
import com.app.library.R
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions


fun View.visible(visible: Boolean? = null): Boolean {
    if (visible != null) {
        visibility = if (visible) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }
    return visibility == View.VISIBLE

}

/**
 * 设置顶部View margin状态栏高度
 *
 */
fun View.fitStatusMargin(offset: Float = 0f) {
    val statusHeight = BarUtils.getStatusBarHeight()
    val lp = layoutParams as MarginLayoutParams
    lp.setMargins(0, statusHeight + SizeUtils.dp2px(offset), 0, 0)
    layoutParams = lp
}

/**
 * 设置顶部View padding状态栏高度
 *
 */
fun View.fitStatusPadding(offset: Float = 0f) {
    val statusHeight = BarUtils.getStatusBarHeight()
    setPadding(0, statusHeight + SizeUtils.dp2px(offset), 0, 0)
}

fun ImageView.loadAvatarUrl(url: String?, default: Int = 0) {
    if (url == null) {
        setImageResource(default)
    } else {
        val mAvatarOptions =
            RequestOptions.bitmapTransform(CircleCrop()).placeholder(default)
                .error(default)
        Glide.with(context)
            .load(url)
            .apply(mAvatarOptions)
            .into(this)
    }
}

fun ImageView.loadRoundUrl(
    url: String?,
    radius: Int = 0,
    holder: Int? = null,
    isGif: Boolean = false
) {
    val option = if (radius != 0 && !isGif)
        RequestOptions().transforms(CenterCrop(), RoundedCorners(SizeUtils.dp2px(radius.toFloat())))
    else
        RequestOptions()
    if (holder != null && holder != 0) {
        option.placeholder(holder)
    }
    if (url == null) {
        setImageResource(holder ?: 0)
    } else {
        if (isGif) {
            Glide.with(context)
                .asGif()
                .load(url)
                .apply(option)
                .into(this)
        } else {
            Glide.with(context)
                .asBitmap()
                .load(url)
                .transition(BitmapTransitionOptions.withCrossFade(300))
                .apply(option)
                .into(this)
        }
    }
}

fun ImageView.loadImageUrl(
    url: String?,
    placeHolder: Int? = null,
    error: Int? = null,
    isFade: Boolean = true
) {
    if (url == null) {
        setImageResource(error ?: placeHolder ?: 0)
    } else {
        val option = RequestOptions()
        placeHolder?.let {
            option.placeholder(it)
        }
        error?.let {
            option.error(error)
        }
        if (isFade) {
            Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(option)
                .transition(BitmapTransitionOptions.withCrossFade(300))
                .into(this)
        } else {
            Glide.with(context)
                .load(url)
                .apply(option)
                .into(this)
        }
    }
}